package com.lanelu.hencoderplusstudy.view

import android.content.Context
import android.graphics.*
import android.util.AttributeSet
import android.view.View
import kotlin.math.cos
import kotlin.math.sin

private var RADIUS = 150f.dp//半径
private var LENGTH = 120f.dp//指针长度
private const val OPEN_ANGLE = 150f//开口角度
private val DASH_WIDTH = 2f.dp//刻度宽度
private val DASH_LENGTH = 10f.dp//刻度长度

/**
 * 仪表盘View练习
 */
class DashboardView(context: Context?, attrs: AttributeSet?) : View(context, attrs) {

    private val paint = Paint(Paint.ANTI_ALIAS_FLAG);//开启抗锯齿
    private val dash = Path()//刻度
    private val path = Path()//弧线
    private lateinit var pathEffect: PathEffect
    private var mark = 0

    init {
        paint.strokeWidth = 3f.dp
        paint.style = Paint.Style.STROKE
        dash.addRect(0f, 0f, DASH_WIDTH, DASH_LENGTH, Path.Direction.CCW)
    }

    override fun onSizeChanged(w: Int, h: Int, oldw: Int, oldh: Int) {
        super.onSizeChanged(w, h, oldw, oldh)
        RADIUS = width / 3f
        LENGTH = RADIUS * 3 / 4
        path.reset()
//        path.addArc(
//            width / 2f - RADIUS,
//            height / 2f - RADIUS,
//            width / 2f + RADIUS,
//            height / 2f + RADIUS,
//            90 + OPEN_ANGLE / 2f,
//            360 - OPEN_ANGLE,
//        )

        path.addArc(
            width / 2f - RADIUS,
            0f,
            width / 2f + RADIUS,
            RADIUS * 2,
            90 + OPEN_ANGLE / 2f,
            360 - OPEN_ANGLE,
        )

        val pathMeasure = PathMeasure(path, false)//forceClosed用来设置当前轮廓是否自动闭合

        //测量刻度间距
        pathEffect = PathDashPathEffect(
            dash,
            (pathMeasure.length - DASH_WIDTH) / 20f,
            0f,
            PathDashPathEffect.Style.ROTATE
        )//advance代表提前量，phase代表间隔，但是方法中advance和phase的值搞反了，所以两个值要反过来写

//        pathMeasure.length
//        pathMeasure.getPosTan()//正切值
////        path.fillType = Path.FillType.WINDING//根据顺时针逆时针判断
//        path.fillType = Path.FillType.EVEN_ODD//根据焦点数判断

        mark = 13
    }

    override fun onDraw(canvas: Canvas) {
//        super.onDraw(canvas)
        canvas.save()
        paint.color = Color.parseColor("#000000")
        //画弧
        canvas.drawPath(path, paint)
        //画刻度
        paint.pathEffect = pathEffect
        canvas.drawPath(path, paint)
        paint.pathEffect = null

        //5
        paint.color = Color.parseColor("#FF0000")
//        canvas.drawLine(
//            width / 2f,
//            height / 2f,
//            width / 2f + LENGTH * cos(markToRadians(mark)).toFloat(),//Math.toRadians 角度转弧度
//            height / 2f + LENGTH * sin(markToRadians(mark)).toFloat(),
//            paint
//        )

        canvas.drawLine(
            width / 2f,
            RADIUS,
            width / 2f + LENGTH * cos(markToRadians(mark)).toFloat(),//Math.toRadians 角度转弧度
            RADIUS + LENGTH * sin(markToRadians(mark)).toFloat(),
            paint
        )
        canvas.restore()
    }

    private fun markToRadians(mark: Int) =
        Math.toRadians((90 + OPEN_ANGLE / 2f + (360 - OPEN_ANGLE) / 20f * mark).toDouble())


}